package models

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type Student struct {
	Sno          string     `gorm:"column:sno"`
	Sname        string     `gorm:"column:sname"`
	Sclass       string     `gorm:"column:sclass"`
	Smajor       string     `gorm:"column:smajor"`
	Sdept        string     `gorm:"column:sdept"`
	Mbti_outcome string     `gorm:"column:mbti_outcome"`
	TestAnswer TestAnswer `gorm:"embedded"`
	FormatTime string
}

func GetAllStudents(studentParam map[string]interface{}) (error, []Student, int64) {
	var allStudents []Student
	page := studentParam["page"].(int)
	pageSize := studentParam["limit"].(int)
	searchText := ""
	fmt.Println(studentParam["Sname"])
	if(studentParam["Sname"] != ""){
		searchText += "\"Student\".\"sname\" like '%"+ studentParam["Sname"].(string)+"%'"
	}
	var total int64
	if (searchText != "") {
	    err := db.Table("Student").Select("\"Student\".sno, sname, sclass, smajor, sdept, mbti_outcome, \"Test_answer_sheet\".tas_id, \"Test_answer_sheet\".test_time, \"Test_answer_sheet\".result").Joins("left join \"Test_answer_sheet\" on \"Test_answer_sheet\".sno = \"Student\".sno").Where(searchText).Order("sno ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&allStudents).Error
	    for i := 0; i < len(allStudents); i++ {
        	allStudents[i].FormatTime = allStudents[i].TestAnswer.Test_time.Format("2006-01-02 15:04:05")
        }
	    return err, allStudents, total
	}else {
	    err := db.Table("Student").Select("\"Student\".sno, sname, sclass, smajor, sdept, mbti_outcome, \"Test_answer_sheet\".tas_id, \"Test_answer_sheet\".test_time, \"Test_answer_sheet\".result").Joins("left join \"Test_answer_sheet\" on \"Test_answer_sheet\".sno = \"Student\".sno").Order("sno ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&allStudents).Error
	    for i := 0; i < len(allStudents); i++ {
        	allStudents[i].FormatTime = allStudents[i].TestAnswer.Test_time.Format("2006-01-02 15:04:05")
        }
	    return err, allStudents, total
	}
}

func AddStudent(a *gin.Context) (error, Student) {
	var studentInfo Student
	a.BindJSON(&studentInfo)
	err := db.Table("Student").Select("sno", "sname", "sclass", "smajor", "sdept", "mbti_outcome").Create(&studentInfo).Error
	return err, studentInfo
}

// used for UpsertStudent
func _AddStudent(studentInfo Student) (error, Student) {
	err := db.Table("Student").Select("sno", "sname", "sclass", "smajor", "sdept", "mbti_outcome").Create(&studentInfo).Error
	return err, studentInfo
}

// used for UpsertStudent
func _UpdateStudent(studentInfo Student) (error, Student) {
	err := db.Table("Student").Where("sno = ?", studentInfo.Sno).Updates(&studentInfo).Error
	return err, studentInfo
}

func UpsertStudent(c *gin.Context) (error, Student) {
	var studentInfo Student
	var result Student
	var total int64
	c.BindJSON(&studentInfo)
	err := db.Table("Test_answer_sheet").Where("sno = ?", studentInfo.Sno).Find(&result).Count(&total).Error
	if total > 0 {
		fmt.Println("修改学生")
		_UpdateStudent(studentInfo)
	} else {
		fmt.Println("添加学生")
		_AddStudent(studentInfo)
	}
	return err, studentInfo
}

func UpdateStudent(a *gin.Context) (error, Student) {
	var studentInfo Student
	a.BindJSON(&studentInfo)
	err := db.Table("Student").Where("sno = ?", studentInfo.Sno).Updates(&studentInfo).Error
	return err, studentInfo
}

func UpdateByParam(studentInfo Student) (error, Student) {
	err := db.Table("Student").Where("sno = ?", studentInfo.Sno).Updates(&studentInfo).Error
	return err, studentInfo
}
